home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / Snippets / Platforms & Tools / ◊Other / TRandom 1.0b3 / Random.h < prev    next >
Encoding:
Text File  |  1991-11-04  |  3.5 KB  |  140 lines  |  [TEXT/MPS ]

  1. /*
  2.     Apple Macintosh Developer Technical Support, 
  3.     TRandom 1.0 -  Saturday, October 5, 1991 18:26:35
  4.  
  5.     TRandom.h    -    C++ source
  6.  
  7.     Copyright © 1991 Apple Computer, Inc. 
  8.     All rights reserved.
  9.  
  10.     TRandom is a stackbased utility class for random number generation.
  11.     TRandom.h contains the header file information for the class.
  12. */
  13.  
  14.  
  15.  
  16. // include it only once
  17. #pragma once
  18.  
  19. //    INCLUDE FILES
  20. #include <Types.h>
  21. #include <OSUtils.h>
  22. #include <Quickdraw.h>
  23. #include <Events.h>
  24. #include <CursorCtl.h>
  25. #include <stream.h>
  26. #include <stdlib.h>
  27.  
  28.  
  29. //     EXTERN FUNCTIONS
  30. extern "C"{
  31.     pascal short AsmRandom();
  32. };
  33.  
  34.  
  35. //    CLASS INTERFACE
  36.  
  37. class TRandom {                                    // Random number class 
  38. public:
  39. //    TYPEDEFS AND ENUMS
  40.     enum eRandType {MACOS,QUICK,SHUFFLE,PM};    // selected random number generator
  41.  
  42. //    CONSTRUCTORS
  43.     TRandom(eRandType = MACOS, long theSeed = 1,// default constructor
  44.             unsigned short low = 0, unsigned short high = 100); 
  45.     TRandom(const TRandom&);                    // copy constructor
  46.     TRandom& operator=(const TRandom&);            // assignment operator
  47.     virtual         ~TRandom();                    // virtual destructor
  48.  
  49. //  MAIN INTERFACES
  50.     unsigned short    Next();                        // get next random number
  51.     TRandom&         ShuffleSeed();                // set seed value
  52.     TRandom&        SetRandomGenerator(eRandType);    // select random number algorithm
  53.     
  54. //    PUBLIC ACCESSORS AND MUTATORS
  55.     long             GetSeedValue() const;
  56.     TRandom&         SetSeedValue(const long theSeed);
  57.     eRandType        GetAlgorithmType() const;
  58.  
  59. //    TESTING
  60.     static void        TestRandomClass();            // static function with test code
  61.     static void        CalcTime(const char*);        // calculate time during test
  62.     
  63. protected:
  64. //    INITIATION ROUTINES
  65.     Boolean         IRandom(eRandType);            // initialize needed class information
  66.     
  67. //    RANDOM GENERATOR ALGORITHMS/MEMBER FUNCTIONS
  68.     unsigned short     MacRandom();                // Toolbox Random function
  69.     unsigned short     QuickRandom();                // quick assembly version
  70.     unsigned short    ShuffleRandom();            // shuffle Toolbox random values
  71.     TRandom&        InitShuffleRandom();        // initialize the shuffle algorithm
  72.     unsigned short     ParkMiller();                // Park&Miller Random generator
  73.     
  74.  
  75. private:
  76. //    TYPEDEFS AND ENUMS (INTERNAL IMPLEMENTATION)
  77.     enum eSIZE         { eSHUFFLETABLE = 128,  ePM1 = 2836, ePM2 = 16807, e16BIT = 65536,
  78.                     ePM3 = 127773, eMSEED = 1618003398, eBIG = 1000000000, 
  79.                     eACM_MAX = 2147483647 }; 
  80.                     
  81.     typedef unsigned short     (TRandom::*RandGen)();        
  82.                                                 // ptr to actual selected random routine
  83.  
  84. //    FIELDS
  85.     RandGen            fGenerator;                    // selected random generator function
  86.     eRandType        fAlgorithm;                    // selected algorithm
  87.     unsigned short    fLow;                        // lower limit of random number
  88.     unsigned short    fHigh;                        // higher limit of random number
  89.     unsigned short     fRange;                        // max size random number
  90.     long            fSeed;                        // random generator seed
  91.     Boolean            fState;                        // current state of object, OK or BAD
  92.     unsigned short    fPrevNum;                    // previous random number
  93.     unsigned short    fShuffleBuf[eSHUFFLETABLE];    // buffer for the shuffle algorithm
  94. };
  95.  
  96.  
  97. // INLINE METHODS
  98.  
  99. inline unsigned short 
  100. TRandom::Next()                     
  101. {     
  102.     return (this->*fGenerator)(); 
  103. }
  104.  
  105. inline TRandom& 
  106. TRandom::ShuffleSeed()                
  107. {     
  108.     this->fSeed = TickCount();
  109.     return *this;
  110. }
  111.  
  112. inline TRandom& 
  113. TRandom::SetRandomGenerator(eRandType theType) 
  114. {     
  115.     this->IRandom(theType);
  116.     return *this;
  117. }
  118.  
  119. inline long 
  120. TRandom::GetSeedValue() const         
  121. {     
  122.     return this->fSeed; 
  123. }
  124.  
  125. inline TRandom& 
  126. TRandom::SetSeedValue(const long theSeed) 
  127. {     
  128.     this->fSeed = theSeed;
  129.     qd.randSeed = theSeed;
  130.     return *this;
  131. }
  132.  
  133.  
  134. inline TRandom::eRandType
  135. TRandom::GetAlgorithmType() const    
  136. {     
  137.     return this->fAlgorithm;
  138. }
  139.  
  140.